package net.intellect.collectPro.businessLogic.payments;

import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;

import net.intellect.force.ClientSalesforce;
import net.intellect.force.Constants;

import com.sforce.soap.enterprise.sobject.CP_Payment__c;
import com.sforce.soap.enterprise.sobject.Past_Due__c;

public class InterestFreePaymentMethod extends BasePaymentMethod implements PaymentMethodProcess{
	
	/**
	 * Apply a PaymentMethod (Interest to the PastDue 
	 *
	 * @param past_due pastDue
	 * @param payment Payments
	 * @throws Exception
	 */
	public void apply(Past_Due__c past_due, List<CP_Payment__c> paymentList) throws Exception {
		if (null != paymentList){			
			consolidatedPayments(past_due, paymentList);
			
			Calendar dateCal = Calendar.getInstance();

			//OPEN --> RECOVERED
			if (past_due.getPast_Due_Amount_Local__c() >= past_due.getMinimum_Payment_Local__c() &&
				past_due.getPast_Due_Amount_USD__c() >= past_due.getMinimum_Payment_USD__c()){
				
				Past_Due__c past_dueUpd = new Past_Due__c();
				past_dueUpd.setId(past_due.getId());
				past_dueUpd.setStatus__c(Constants.PAST_DUE_STATUS_RECOVERED);
				past_dueUpd.setRecovery_Date__c(dateCal.getTime());
				
				try{
					ClientSalesforce.update(past_dueUpd);
				} catch(Exception e) { 
					logger.log(Level.INFO, "COLLECT/PAYMENTS Error InterestFreePayment Method update Open to Recover Past Due Status " +past_due.getId() + "  - End of process");
					throw new Exception("6.1 Error InterestFreePayment Method update Open to Recover the Past Due Status " +past_due.getId() + "  - End of process");
				}
				//updateActivityPast_due(past_due,PAST_DUE_STATUS_RECOVERED);	
			//CLOSED or RECOVERED --> OPEN
			}else if (past_due.getPast_Due_Amount_Local__c() < past_due.getMinimum_Payment_Local__c() ||
					  past_due.getPast_Due_Amount_USD__c() < past_due.getMinimum_Payment_USD__c()){

				if (!Constants.PAST_DUE_STATUS_OPEN.equals(past_due.getStatus__c())){
					Past_Due__c past_dueUpd = new Past_Due__c();
					past_dueUpd.setId(past_due.getId());
					past_dueUpd.setStatus__c(Constants.PAST_DUE_STATUS_OPEN);	
					past_dueUpd.addFieldsToNull("Recovery_Date__c");					
					
					try{
						ClientSalesforce.update(past_dueUpd);
					} catch(Exception e) { 
						logger.log(Level.INFO, "COLLECT/PAYMENTS Error InterestFreePayment Method update Recover to Open Past Due Status " +past_due.getId() + "  - End of process");
						throw new Exception("6.2 Error InterestFreePayment Method update Recover to Open the Past Due Status " +past_due.getId() + "  - End of process");
					}
					
					//updateActivityPast_due(past_due,PAST_DUE_STATUS_OPEN);
				}				
			}else if (!Constants.PAST_DUE_STATUS_CLOSED.equals(past_due.getStatus__c())){
				logger.log(Level.INFO, "COLLECT/PAYMENTS InterestFreePayment Method Past Due Closed: " +past_due.getId() + " and have payments opens  - End of process");
			}
		}			
	}

}
